package com.hospital.hospitalsystemgateway.config;

import com.hospital.hospitalsystemgateway.modules.dto.UserExt;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

/**
 * @author qiancheng@cqxyy.net
 * @date 2023/2/21
 */
public class TokenConfig {

    //token密钥
    private static final String TOKEN_SECRET = "LJSBF932GFH9WF9WEGFFGWEPUFWPUFG3Q3D";


    /**
     * token解密
     * @param token
     * @return
     */
    public static UserExt tokenToOut(String token) throws Exception{
        Claims claims = Jwts.parser()
                .setSigningKey(TOKEN_SECRET)
                .parseClaimsJws(token)
                .getBody();
        UserExt userExt = new UserExt();
        userExt.setUserId(claims.getId());
        userExt.setUserName(claims.getSubject());
        userExt.setUserTime(claims.getIssuedAt());
        userExt.setOverdueTime(claims.getExpiration());
        userExt.setRoles((String)claims.get("role"));
        return userExt;
    }
}
